package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.annotation.Auth;
import com.example.demo.enums.UserPermission;
import com.example.demo.service.interfaces.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Slf4j
@RestController
public class TestLoginController {
    @Autowired
    LoginService loginService;

    @GetMapping("/api/config/login/{rw}")
    public R testLogin(HttpServletRequest request, HttpServletResponse response, @PathVariable String rw) {
        if (UserPermission.WRITE.name().equals(rw)) {
            loginService.addUser(request, response, "123456", Arrays.asList(UserPermission.WRITE));
        } else if (UserPermission.READ.name().equals(rw)) {
            loginService.addUser(request, response, "123456", Arrays.asList(UserPermission.READ));
        } else {
            return R.failed("参数错误");
        }
        return R.ok("success");
    }

    @GetMapping("/api/config/test1")
    public R testAuth1() {
        return R.ok("success");
    }

    @GetMapping("/api/config/test2")
    @Auth
    public R testAuth2() {
        return R.ok("success");
    }

    @GetMapping("/api/config/test3")
    @Auth(UserPermission.READ)
    public R testAuth3() {
        return R.ok("success");
    }
}
